<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;



//后台模块表
class CustomerModel extends Model
{


	//定义一个表名
	protected $table = 'customer';
	protected $guarded = [];

	//定义26字母
	static function letter($uid,$keywords){
		$letter = [
			[
				'letter' => 'A',
				'user' => CustomerModel::letter_re('A',$uid,$keywords),
			],
			[
				'letter' => 'B',
				'user' => CustomerModel::letter_re('B',$uid,$keywords),
			],
			[
				'letter' => 'C',
				'user' => CustomerModel::letter_re('C',$uid,$keywords),
			],
			[
				'letter' => 'D',
				'user' => CustomerModel::letter_re('D',$uid,$keywords),
			],
			[
				'letter' => 'E',
				'user' => CustomerModel::letter_re('E',$uid,$keywords),
			],
			[
				'letter' => 'F',
				'user' => CustomerModel::letter_re('F',$uid,$keywords),
			],
			[
				'letter' => 'G',
				'user' => CustomerModel::letter_re('G',$uid,$keywords),
			],
			[
				'letter' => 'H',
				'user' => CustomerModel::letter_re('H',$uid,$keywords),
			],
			[
				'letter' => 'I',
				'user' => CustomerModel::letter_re('I',$uid,$keywords),
			],
			[
				'letter' => 'J',
				'user' => CustomerModel::letter_re('J',$uid,$keywords),
			],
			[
				'letter' => 'K',
				'user' => CustomerModel::letter_re('K',$uid,$keywords),
			],
			[
				'letter' => 'L',
				'user' => CustomerModel::letter_re('L',$uid,$keywords),
			],
			[
				'letter' => 'M',
				'user' => CustomerModel::letter_re('M',$uid,$keywords),
			],
			[
				'letter' => 'N',
				'user' => CustomerModel::letter_re('N',$uid,$keywords),
			],
			[
				'letter' => 'O',
				'user' => CustomerModel::letter_re('O',$uid,$keywords),
			],
			[
				'letter' => 'P',
				'user' => CustomerModel::letter_re('P',$uid,$keywords),
			],
			[
				'letter' => 'Q',
				'user' => CustomerModel::letter_re('Q',$uid,$keywords),
			],
			[
				'letter' => 'R',
				'user' => CustomerModel::letter_re('R',$uid,$keywords),
			],
			[
				'letter' => 'S',
				'user' => CustomerModel::letter_re('S',$uid,$keywords),
			],
			[
				'letter' => 'T',
				'user' => CustomerModel::letter_re('T',$uid,$keywords),
			],
			[
				'letter' => 'U',
				'user' => CustomerModel::letter_re('U',$uid,$keywords),
			],
			[
				'letter' => 'V',
				'user' => CustomerModel::letter_re('V',$uid,$keywords),
			],
			[
				'letter' => 'W',
				'user' => CustomerModel::letter_re('W',$uid,$keywords),
			],
			[
				'letter' => 'X',
				'user' => CustomerModel::letter_re('X',$uid,$keywords),
			],
			[
				'letter' => 'Y',
				'user' => CustomerModel::letter_re('Y',$uid,$keywords),
			],
			[
				'letter' => 'Z',
				'user' => CustomerModel::letter_re('Z',$uid,$keywords),
			],
			[
				'letter' => '#',
				'user' => CustomerModel::letter_re('#',$uid,$keywords),
			],
		];

		return $letter;
	}

	static function letter_re($letter,$uid,$words){
		$keywords = '';
		if($words){
			$keywords = $words;
		}
		$cus = [];
		$customer = CustomerModel::where('uid',$uid)->where('head',$letter)->where('name',"like","%".$keywords."%")->orderBy(DB::raw('convert(`name` using gbk)'))->get()->toArray();
		foreach($customer as $k=>$v){
			$cus[$k]['id'] = $v['id'];
			$cus[$k]['name'] = $v['name'];
			$cus[$k]['phone'] = $v['phone'];
		}
		return $cus;
	}

	//获取客户
	static function getList($uid,$words){
		$keywords = '';
		if($words){
			$keywords = $words;
		}
		$cus = [];
		$customer = CustomerModel::where('uid',$uid)->where('name','like',"%".$keywords."%")->get()->toArray();
		foreach($customer as $k=>$v){
			$cus[$k]['id'] = $v['id'];
			$cus[$k]['name'] = $v['name'];
			$cus[$k]['phone'] = $v['phone'];
		}
		return $cus;
	}
}
